import request from '@/utils/request'
import type {
  TenantDTO,
  TenantQueryRequest,
  TenantStatsDTO,
  TenantOptionDTO,
  PageResponse,
  ApiResponse
} from '@/types/api'

// 租户管理API
export const tenantApi = {
  // 创建租户
  createTenant: (data: TenantDTO): Promise<ApiResponse<TenantDTO>> => {
    return request.post('/tenants', data)
  },

  // 更新租户信息
  updateTenant: (tenantId: string, data: TenantDTO): Promise<ApiResponse<TenantDTO>> => {
    return request.put(`/tenants/${tenantId}`, data)
  },

  // 获取租户详情
  getTenant: (tenantId: string): Promise<ApiResponse<TenantDTO>> => {
    return request.get(`/tenants/${tenantId}`)
  },

  // 删除租户（软删除）
  deleteTenant: (tenantId: string): Promise<ApiResponse<void>> => {
    return request.delete(`/tenants/${tenantId}`)
  },

  // 分页查询租户列表
  queryTenants: (params: TenantQueryRequest): Promise<ApiResponse<PageResponse<TenantDTO>>> => {
    return request.get('/tenants', { params })
  },

  // 更新租户状态
  updateTenantStatus: (tenantId: string, status: number): Promise<ApiResponse<void>> => {
    return request.put(`/tenants/${tenantId}/status`, null, {
      params: { status }
    })
  },

  // 获取租户统计信息
  getTenantStats: (tenantId: string): Promise<ApiResponse<TenantStatsDTO>> => {
    return request.get(`/tenants/${tenantId}/stats`)
  },

  // 获取租户选项列表（用于下拉选择）
  getTenantOptions: (): Promise<ApiResponse<TenantOptionDTO[]>> => {
    return request.get('/tenants/options')
  },

  // 启用租户
  enableTenant: (tenantId: string): Promise<ApiResponse<void>> => {
    return tenantApi.updateTenantStatus(tenantId, 1)
  },

  // 禁用租户
  disableTenant: (tenantId: string): Promise<ApiResponse<void>> => {
    return tenantApi.updateTenantStatus(tenantId, 0)
  },

  // 检查租户ID是否可用
  checkTenantIdAvailable: (tenantId: string): Promise<ApiResponse<boolean>> => {
    return request.get('/tenants/check-available', {
      params: { tenantId }
    })
  }
}

export default tenantApi

